package com.highcom.paging;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.highcom.domain.Student;
import com.highcom.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class TestPage {

    @Test
    public void selectStudent(){
        SqlSession sqlSession = null;
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("MybatisConfig.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = sqlSessionFactory.openSession(true);
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            //第一页 显示10条数据
            //PageHelper.startPage(1,10);
            PageHelper.startPage(2,10);
            //调用查询方法
            List<Student> students = mapper.selectAll();
            for (Student s:students){
                System.out.println("student = " + s);

            }

            PageInfo<Student> pageInfo = new PageInfo<>(students);

            System.out.println("总条数" + pageInfo.getTotal());
            System.out.println("总页数" + pageInfo.getPages());
            System.out.println("当前页" + pageInfo.getPageNum());
            System.out.println("每页显示长度" + pageInfo.getPageSize());
            System.out.println("是否是第一页" +pageInfo.isIsFirstPage());
            System.out.println("是否是末页" +pageInfo.isIsLastPage());

        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(sqlSession != null){
                sqlSession.close();
            }
            if(is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}
